/*
 * KoneksiAini.java
 *
 * Created on August 16, 2007, 3:37 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package taini.koneksi;

import com.stemaini.core.doc.Vektor;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import taini.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Development
 */
public class KoneksiAini {
    private Connection conn;
     String ip = "";
    String username = "";
    String pwd = "";
    String dbase = "";

    /** Creates a new instance of KoneksiAini */
    public KoneksiAini() {
        conn = null;
    }
     public void setIP(String namaip)
    {
        this.ip = namaip;
    }
    public void setUser(String namauser)
    {
        this.username = namauser;
       // System.out.print(this.username);
    }
    public void setPass(String pass)
    {
        this.pwd = pass;
      //  System.out.print(this.pwd);
    }
    public void setDB(String namadb)
    {
        this.dbase = namadb;
    }
    public void connectFirst() throws SQLException
    {
        String[] input = new String[2];
        try {

           input = bacafile("testing.txt");
        } catch (IOException ex) {
            Logger.getLogger(KoneksiAini.class.getName()).log(Level.SEVERE, null, ex);
        }
        String ipserver="localhost",user="root",pass="",db="taaini"; //10.151.33.115
        if (isConnected())
        {
        }
        else getConnection(ipserver, user, pass,db);
        
        if (isConnected()){
        //System.out.println("oke konek");

        }         else System.out.println("gak oke konek");
        //cobaselect();
    }
    public boolean getConnection(String ipServer,String username, String password,String db)throws SQLException,IllegalStateException{
        this.destroyConnection();
        String dbDriver = "com.mysql.jdbc.Driver";
        String dbURL = "jdbc:mysql://" +ipServer +"/"+db;
        try {
            Class.forName(dbDriver);
            conn = DriverManager.getConnection(dbURL,username,password);
            return true;
        } catch (ClassNotFoundException ex) {
            //ex.printStackTrace();
        } catch (SQLException ex) {
            //ex.printStackTrace();
        }
        return false;
    }
    
    public void destroyConnection() 
    throws SQLException,IllegalStateException{
        if(conn!=null){
            conn.close();
            conn = null;
        }
    }
    
    public boolean isConnected()
    {
        if(conn!=null)
            return true;
        return false;
    }
    
    public ResultSet executeSelect(String query) throws SQLException,IllegalStateException{
        Statement stm;
        ResultSet rs = null;
        
        if(conn==null)
            return null;
        stm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        rs = stm.executeQuery(query);
        return rs;
    }
    
    public int executeUpdate(String query) throws SQLException,IllegalStateException{
        Statement stmt;
      
        if(conn==null)
            return 0;
        stmt = conn.createStatement();
      
        return stmt.executeUpdate(query);
    }
    
    public PreparedStatement persiapkanStatement(String query) 
    throws SQLException,IllegalStateException{
        PreparedStatement pstmt = conn.prepareStatement(query);
        return pstmt;
    }
    public static void main(String[] args) {
        // TODO code application logic here
        KoneksiAini theKoneksi = new KoneksiAini();
        String[] coba = new String[2];
        try {
            coba = theKoneksi.bacafile("testing.txt");
            System.out.println(coba[0]);
            System.out.println(coba[1]);
            //        try {
            //            theKoneksi.bacafile("testing.txt");
            //        } catch (IOException ex) {
            //            Logger.getLogger(KoneksiAini.class.getName()).log(Level.SEVERE, null, ex);
            //        }
            //        String sql = "SELECT id_term FROM TERMS WHERE nama_term = 'aini'";
            //            try {
            //                theKoneksi.connectFirst();
            //                ResultSet rs = theKoneksi.executeSelect(sql);
            //                if(rs.next())
            //                {
            //                    //vn.setIdTerm(rs.getInt(1));
            //                    System.out.println(rs.getInt(1));
            //                }
            //                else
            //                {
            //                    sql = "SELECT max( id_term ) FROM TERMS";
            //                    rs = theKoneksi.executeSelect(sql);
            //                    if(rs.next())
            //                    {
            //                        int newID = rs.getInt(1)+1;
            //                        System.out.println(newID);
            //                    }
            //                    //vn.setIdTerm(newID+1);
            //                }
            //            } catch (SQLException ex) {
            //                Logger.getLogger(Vektor.class.getName()).log(Level.SEVERE, null, ex);
            //            } catch (IllegalStateException ex) {
            //            }
            //            }
        } catch (IOException ex) {
            Logger.getLogger(KoneksiAini.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String[] bacafile(String filename) throws IOException {
          String[] buf = new String[2];

   try {
    BufferedReader ins = new BufferedReader(new FileReader(filename));
    String str;
    int i = 0;
    while ((str = ins.readLine()) != null) {
        buf[i] = str;
       // System.out.println(buf[i]);
        i++;
    }


    //in.close();
} catch (IOException e) {
}
return buf;
    }
}
